<?php
$ROOT_DIR_PATH = '../';
require_once($ROOT_DIR_PATH.'includes/includes.php');
require_once($ROOT_DIR_PATH.'classes/class.preference.inc.php');require_once($ROOT_DIR_PATH.'classes/class.settings.inc.php');
require_once($ROOT_DIR_PATH.'classes/class.user.inc.php');
$loginUrl = $ADMIN_BASE_HREF . "login.php";
$dashBoardUrl = $ADMIN_BASE_HREF . "dashboard.php";$settingsObj = new settings();$settingsArray = $settingsObj->getAllSettingsByName();$contact_name = $settingsArray['contact_name'];$contact_emailadress1 = $settingsArray['contact_emailadress1'];$contact_emailadress2 = $settingsArray['contact_emailadress2'];$contact_telephone = $settingsArray['contact_telephone'];$contact_address = $settingsArray['contact_address'];$social_googleplus = $settingsArray['social_googleplus'];$social_youtube = $settingsArray['social_youtube'];$social_pinterest = $settingsArray['social_pinterest'];$social_facebook = $settingsArray['social_facebook'];$social_twitter = $settingsArray['social_twitter'];
if (!$GENOBJ->isLoggedIn()) {
    $GENOBJ->redirect($loginUrl);
}
if(!$GENOBJ->isAdminLogin()) {
	$GENOBJ->redirect($dashBoardUrl);
}

$userObj = new user();
$userData = $userObj->select("0");
$paging_limit = '';
$company_name = '';
$company_url = '';
$user_name = '';



$ADMIN_PAGE_TITLE = 'Preference';
$GENOBJ->clearSessionErrorMessages();
$preferenceObj = new preference();
$preferenceData = $preferenceObj->select();

$paging_limit = $preferenceData['paging_limit'];
$company_name = $preferenceData['company_name'];
$company_url = $preferenceData['company_url'];
$user_name = $userData['username'];
$errorMessageArray = Array();
$succMessageArray = Array();
if ($_POST) {
    $filterElements = Array();
    $filterArray = Array("TrimElement", "htmlEntity");

    $paging_limit = $_POST['paging_limit'];
    $paging_limit = $GENOBJ->filter($paging_limit, $filterArray);

    $company_name = $_POST['company_name'];
    $company_name = $GENOBJ->filter($company_name, $filterArray);

    $company_url = $_POST['company_url'];
    $company_url = $GENOBJ->filter($company_url, $filterArray);

    $user_name  = $_POST['admin_name'];
    $user_name  = $GENOBJ->filter($user_name , $filterArray);
		$contact_name = $_POST['contact_name'];	$contact_name = $GENOBJ->filter($contact_name, $filterArray);	$contact_emailadress1 = $_POST['contact_emailadress1'];	$contact_emailadress1 = $GENOBJ->filter($contact_emailadress1, $filterArray);	$contact_emailadress2 = $_POST['contact_emailadress2'];	$contact_emailadress2 = $GENOBJ->filter($contact_emailadress2, $filterArray);	$contact_telephone = $_POST['contact_telephone'];	$contact_telephone = $GENOBJ->filter($contact_telephone, $filterArray);	$contact_address = $_POST['contact_address'];	$contact_address = $GENOBJ->filter($contact_address, $filterArray);	$social_googleplus = $_POST['social_googleplus'];	$social_googleplus = $GENOBJ->filter($social_googleplus, $filterArray);	$social_youtube = $_POST['social_youtube'];	$social_youtube = $GENOBJ->filter($social_youtube, $filterArray);	$social_facebook = $_POST['social_pinterest'];	$social_facebook = $GENOBJ->filter($social_facebook, $filterArray);	$social_facebook = $_POST['social_facebook'];	$social_facebook = $GENOBJ->filter($social_facebook, $filterArray);	$social_twitter = $_POST['social_twitter'];	$social_twitter = $GENOBJ->filter($social_twitter, $filterArray);			
    if ($GENOBJ->IsEmpty($company_name)) {
        $errorMessageArray[] = 'Company name shouldnot be blank.';
    }

    if ($GENOBJ->IsEmpty($user_name)) {
        $errorMessageArray[] = 'Admin name shouldnot be blank.';
    }

    if ($GENOBJ->IsEmpty($company_url)) {
        $errorMessageArray[] = 'Company url shouldnot be blank.';
    } else if (!$GENOBJ->ValidUrl($company_url)) {
        $errorMessageArray[] = 'Company url is not valid url';
    }

    if ($GENOBJ->IsEmpty($paging_limit)) {
        $errorMessageArray[] = 'Please enter paging limit for each page.';
    } else {
        if (!$GENOBJ->isNumeric($paging_limit)) {
            $errorMessageArray[] = 'Please enter only valid numeric value for paging.';
        }
        if ($GENOBJ->isNumeric($paging_limit)) {
            if ($paging_limit <= 0) {
                $errorMessageArray[] = 'Paging limit should be greater than zero.';
            }
        }
    }

    if (count($errorMessageArray) <= 0) {
        $preferenceObj->setpaging_limit($paging_limit);
        $preferenceObj->setcompany_name($company_name);
        $preferenceObj->setcompany_url($company_url);
        $preferenceObj->update();
        
        $updateSql = "UPDATE `user` SET   `username` = '".$user_name."' WHERE `userid` = '0'";
        $DBOBJ->sql_query($updateSql);				if ($contact_name != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$contact_name."' WHERE `name` = 'contact_name';";			$DBOBJ->sql_query($updateSql);		}		if ($contact_emailadress1 != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$contact_emailadress1."' WHERE `name` = 'contact_emailadress1';";			$DBOBJ->sql_query($updateSql);		}		if ($contact_emailadress2 != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$contact_emailadress2."' WHERE `name` = 'contact_emailadress2';";			$DBOBJ->sql_query($updateSql);		}		if ($contact_telephone != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$contact_telephone."' WHERE `name` = 'contact_telephone';";			$DBOBJ->sql_query($updateSql);		}		if ($contact_address != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$contact_address."' WHERE `name` = 'contact_address';";			$DBOBJ->sql_query($updateSql);		}		if ($social_googleplus != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$social_googleplus."' WHERE `name` = 'social_googleplus';";			$DBOBJ->sql_query($updateSql);		}		if ($social_youtube != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$social_youtube."' WHERE `name` = 'social_youtube';";			$DBOBJ->sql_query($updateSql);		}		if ($social_pinterest != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$social_pinterest."' WHERE `name` = 'social_pinterest';";			$DBOBJ->sql_query($updateSql);		}		if ($social_facebook != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$social_facebook."' WHERE `name` = 'social_facebook';";			$DBOBJ->sql_query($updateSql);		}		if ($social_twitter != '') {			$updateSql = "UPDATE `settings` SET `value` = '".$social_twitter."' WHERE `name` = 'social_twitter';";			$DBOBJ->sql_query($updateSql);		}								
		$_SESSION[SESSION_NAME]['admin']['username'] = $user_name;
        $succMessageArray[] = 'Your Preferences changed successfully!';
        $_SESSION[SESSION_NAME]['admin']['username'] = $user_name;
    }
}
?>
<?php include('templates/header.php');?>
<body>
    <div id="preference"></div>
	<div id="ncleangrey-container">
		<?php include('templates/navigation.php');?>
		<div class="breadcrumbs">
			<div id="nav-icons_all">
				<ul id="nav-icons">
					<li class="logout-icon"><a  title="Logout"  href="logout.php">Logout</a></li>
				</ul>
			</div>
               <p class="breadcrumbs">
                    <a title="Home" href="dashboard.php">Home</a> &#187; My Preferences</a>
               </p>
		</div>
		<div class="hstippled">&nbsp;</div>
		<div id="MainContent">
			<div>
				<div class="pageerrorcontainer-validator">
					<div class="error-iterator"></div>
				</div>
			</div>
          <?php if(count($succMessageArray) > 0):?>
               <div class="pagemcontainer">
               	<?php foreach($succMessageArray as $succValue):?>
				<p class="pagemessage">
					<img src="images/icons/system/accept.gif" class="systemicon" alt="Success" title="Success" />
					<label class="errorlabel"><?php echo $succValue;?></label>
				</p>
				<?php endforeach;?>
			</div>
			<?php endif;?>
               <div class="clearb">&nbsp;</div>
		<?php if(count($errorMessageArray) > 0):?>
			<div class="pageerrorcontainer">
			<?php foreach($errorMessageArray as $erroValue):?>
				<div class="pageoverflow">
					<img src="images/icons/system/stop.gif" class="systemicon" alt="Error" title="Error" />
					<label class="errorlabel"><?php echo $erroValue;?></label>
				</div>
			<?php endforeach;?>
			</div>
		<?php endif;?>
               <div class="clearb">&nbsp;</div>
               <div id="Edit_Content_Result"></div>
               <div class="pagecontainer pageoverflow">
                    <div class="pageheader">
                      Change Preferences
                      <div class="field-required">* Fields are required </div>
                    </div>
                    <div style="clear: both;"></div>
                    <form method="post" action="preference.php" name="preferencefrm" id="preferencefrm">
                    	<div id="page_content">

                            <div class="pageoverflow">
                                   <div class="pagetext">Admin User Name<span class="error-star">*</span>:</div>
                                   <div class="pageinput">
                                        <input type="text" name="admin_name" id="admin_name" value="<?php echo $user_name;?>" size="60"/>
                                   </div>
                              </div>

                              <div class="pageoverflow">
                                   <div class="pagetext">Company Name<span class="error-star">*</span>:</div>
                                   <div class="pageinput">
                                        <input type="text" name="company_name" id="company_name" value="<?php echo $company_name;?>" size="60"/>
                                   </div>
                              </div>

                                <div class="pageoverflow">
                                   <div class="pagetext">Company Url<span class="error-star">*</span>:</div>
                                   <div class="pageinput">
                                        <input type="text" name="company_url" id="company_url" value="<?php echo $company_url;?>" size="60"/>
                                   </div>
                                </div>																<div class="pageoverflow">									<div class="pagetext">Contact Name<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="contact_name" id="contact_name" value="<?php echo $contact_name;?>" size="60"/>									</div>								</div>																<div class="pageoverflow">									<div class="pagetext">Contact Emailadress1<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="contact_emailadress1" id="contact_emailadress1" value="<?php echo $contact_emailadress1;?>" size="60"/>									</div>								</div>																<div class="pageoverflow">									<div class="pagetext">Contact Emailadress2<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="contact_emailadress2" id="contact_emailadress2" value="<?php echo $contact_emailadress2;?>" size="60"/>									</div>								</div>																<div class="pageoverflow">									<div class="pagetext">Contact Telephone<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="contact_telephone" id="contact_telephone" value="<?php echo $contact_telephone;?>" size="60"/>									</div>								</div>																																<div class="pageoverflow">									<div class="pagetext">Contact Address:</div>									<div class="pageinput">										<textarea rows="10" cols="50" name="contact_address" id="contact_address"><?php echo $contact_address;?></textarea>									</div>								</div>								<div class="pageoverflow">									<div class="pagetext">Google plus link<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="social_googleplus" id="social_googleplus" value="<?php echo $social_googleplus;?>" size="60"/>									</div>								</div>																<div class="pageoverflow">									<div class="pagetext">You tube link<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="social_youtube" id="social_youtube" value="<?php echo $social_youtube;?>" size="60"/>									</div>								</div>																<div class="pageoverflow">									<div class="pagetext">Pinterest Link<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="social_pinterest" id="social_pinterest" value="<?php echo $social_pinterest;?>" size="60"/>									</div>								</div>																<div class="pageoverflow">									<div class="pagetext">Facebook Link<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="social_facebook" id="social_facebook" value="<?php echo $social_facebook;?>" size="60"/>									</div>								</div>																<div class="pageoverflow">									<div class="pagetext">Twitter Link<span class="error-star">*</span>:</div>									<div class="pageinput">										<input type="text" name="social_twitter" id="social_twitter" value="<?php echo $social_twitter;?>" size="60"/>									</div>								</div>								                              <div class="pageoverflow">
                                   <div class="pagetext">Paging Per Page<span class="error-star">*</span>:</div>
                                   <div class="pageinput">
                                        <input type="text" name="paging_limit" id="paging_limit" value="<?php echo $paging_limit;?>" size="30"/>
                                   </div>
                              </div>
                              <div class="pageoverflow">
                                   <p class="pagetext">&nbsp;</p>
                                   <p class="pageinput">
                                        <input type="submit" name="submitbutton" value="Submit" title="Save changes" />
                                        <input type="reset" name="apply" value="Reset" title="Save changes and continue to edit" />
                                   </p>
                              </div>
                              <div style="clear: both;"></div>
                         </div>
                    </form>
               </div>
               <p class="pageback">
				<a class="pageback" href="dashboard.php">&#171; Back to Home</a>
			</p>
               <div class="clearb"></div>
          </div>
     </div>
     <?php include('templates/footer.php');?>
</body>
</html>